package com.itheima.service;

import com.itheima.mapper.StatMapeer;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2022/12/17 14:43
 */
@Service
public class StatService {

    @Autowired
    StatMapeer statMapeer;

    @Autowired
    UserMapper userMapper;

    public List<Map> columnCharts() {
        return statMapeer.columnCharts();
    }

    public List<Map> lineCharts() {
        return statMapeer.lineCharts();
    }

    public List<Map<String, Object>> pieCharts() {
        //        最终想要的数据格式：[{id:"河北省",
        // "drilldown":"河北省",
        // "name":"河北省",
        // "y":10,
        // "data":[{"name":"石家庄","y":4},{"name":"唐山","y":3},{"name":"保定","y":3}]}]

        //总集合
        List<Map<String, Object>> pieChartsList = new ArrayList<>();

        //获取数据
        List<User> userList = userMapper.selectAll();

        //下钻饼状图 需要查询出每个省有多少个市区 ，下钻显示每个省市的市区有多少人    要根据顺序
        //首先处理省 先排序
        Map<String, List<User>> provinceMapList = userList.stream()
                .collect(Collectors.groupingBy(User::getProvince));
        //创建一个容器存储省数据
        for (String provinceName : provinceMapList.keySet()) {
            Map<String,Object> provinceCityMap = new HashMap<>();
            provinceCityMap.put("id",provinceName);
            provinceCityMap.put("drilldown",provinceName);
            provinceCityMap.put("name",provinceName);

            //市区数量
            List<User> provinceSize = provinceMapList.get(provinceName);
            provinceCityMap.put("y",provinceSize.size());

            //遍历每个省
            //每个市区进行排序
            Map<String, List<User>> cityMap = provinceSize.stream()
                    .collect(Collectors.groupingBy(User::getCity));
            //拿到cityName
            List<Map> cityMapList =new ArrayList<>();
            for (String cityName : cityMap.keySet()) {
                Map<String,Object> dataMap = new HashMap<>();
                dataMap.put("name",cityName);
                List<User> citySize = cityMap.get(cityName);
                dataMap.put("y",citySize.size());
                cityMapList.add(dataMap);
            }
            //存储data数据
            provinceCityMap.put("data",cityMapList);

            //把封装好的数据存入总集合
            pieChartsList.add(provinceCityMap);
        }
        return pieChartsList;
    }

    public Map<String,List<Map<String,Object>>> pieECharts() {

        Map<String,List<Map<String,Object>>> resultMap = new HashMap();

        //  最终想要的数据格式：{province:[{name:xxx,value:xxx}],city:[{name:xxx,value:xxx}]}
        //echarts下钻饼状图顺序很重要需要先排序
        Example example = new Example(User.class);
        example.setOrderByClause("province , city");
        List<User> userList = userMapper.selectByExample(example);


        //创建容器
        List<Map<String,Object>> provinceList = new ArrayList<>();
        List<Map<String,Object>> cityList = new ArrayList<>();


        //拿出排好序的省  这是一个有序的
        Map<String, List<User>> provinceMap = userList.stream()
                .collect(Collectors.groupingBy(User::getProvince, LinkedHashMap::new, Collectors.toList()));

        for (String provinceName : provinceMap.keySet()) {
            Map<String,Object> province = new HashMap<>();
            province.put("name",provinceName);

            //key找值
            List<User> provinceLists = provinceMap.get(provinceName);
            province.put("value",provinceLists.size());
            provinceList.add(province);

            //取市区
            Map<String, List<User>> cityMaps = provinceLists.stream()
                    .collect(Collectors.groupingBy(User::getCity, LinkedHashMap::new, Collectors.toList()));

            for (String cityName : cityMaps.keySet()) {
                Map<String,Object> city = new HashMap<>();
                city.put("name",cityName);
                city.put("value",cityMaps.get(cityName).size());

                cityList.add(city);
            }
        }
        resultMap.put("province",provinceList);
        resultMap.put("city",cityList);

        return resultMap;
    }
}
